package br.com.ozsolucoes.filters;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import br.com.ozsolucoes.entity.Cliente;

@WebFilter(urlPatterns = "/*", filterName = "FiltroLogin")
public class FiltroLogin implements Filter {

	private static Cliente cliente;
	private static int tipoCliente;
	private static final int PERFIL_CLIENTE = 1;
	private static final int PERFIL_ADMINISTRADOR = 2;
	private static final String ARG_CLIENTE = "cliente";
	private static final String ARG_TIPO = "tipoCliente";
	private static final String PAGINA_LOGIN = "/dinamico/paginas/principal/login.jsf";
	private static final String PAGINA_MEU_PERFIL = "/dinamico/paginas/panelCliente/perfil/meuPerfil.jsf";

	public void destroy() {
	}

	public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
		HttpServletRequest req = (HttpServletRequest) servletRequest;
		HttpSession session = req.getSession();
		try {
			cliente = (Cliente) session.getAttribute(ARG_CLIENTE);
			tipoCliente = (Integer) session.getAttribute(ARG_TIPO);
		} catch (Exception e) {
		}
		
		HttpServletResponse resp = (HttpServletResponse) servletResponse;

		if (provedorPaginasLiberadas(req) || cliente != null) {
			
			if (0 != tipoCliente) {
				if (PERFIL_CLIENTE == tipoCliente) {
					if (!paginasLiberadasCliente(req)) {
						resp.sendRedirect(req.getContextPath() + PAGINA_MEU_PERFIL);
					} else {
						filterChain.doFilter(servletRequest, servletResponse);
					}
				} else if (PERFIL_ADMINISTRADOR == tipoCliente){
					if (!paginasLiberadasAdministrador(req)) {
						resp.sendRedirect(req.getContextPath() + PAGINA_MEU_PERFIL);
					} else {
						filterChain.doFilter(servletRequest, servletResponse);
					}
				}
			} else {
				filterChain.doFilter(servletRequest, servletResponse);
			}
		} else {
			resp.sendRedirect(req.getContextPath() + PAGINA_LOGIN);
		}
	}

	private boolean provedorPaginasLiberadas(HttpServletRequest req) {
		return (req.getRequestURI().endsWith("login.jsf"))
                       || req.getRequestURI().endsWith("cadastrarCliente.jsf")
                       || req.getRequestURI().endsWith("menu.jsf")
                       || req.getRequestURI().contains("javax.faces.resource")
                       || req.getRequestURI().contains("static");
	}

	private boolean paginasLiberadasAdministrador(HttpServletRequest req) {
		return (req.getRequestURI().endsWith("listarAlterarCliente.jsf")
			|| req.getRequestURI().endsWith("consultarCliente.jsf")
			|| req.getRequestURI().endsWith("excluirCliente.jsf")
			|| req.getRequestURI().endsWith("cadastrarCliente.jsf")
			|| req.getRequestURI().endsWith("consultarTicketAlterar.jsf")
			|| req.getRequestURI().endsWith("consultarTicket.jsf")
			|| req.getRequestURI().endsWith("consultarTicketExcluir.jsf")
			|| req.getRequestURI().endsWith("consultarTicketFechar.jsf")
			|| req.getRequestURI().endsWith("consultarContas.jsf")
			|| req.getRequestURI().endsWith("meuPerfil.jsf")
			|| req.getRequestURI().endsWith("alterarCliente.jsf")
			|| req.getRequestURI().endsWith("alterarCliente.jsf")
			|| req.getRequestURI().contains("javax.faces.resource")
            || req.getRequestURI().contains("static")
            || req.getRequestURI().endsWith("index.jsf")
			|| req.getRequestURI().endsWith("consultarContas.jsf")
			|| req.getRequestURI().endsWith("detalharContasCliente.jsf")
			|| req.getRequestURI().endsWith("abrirTicket.jsf")
			|| req.getRequestURI().endsWith("consultarClienteTicket.jsf")
			|| req.getRequestURI().endsWith("sucessoAbeturaTicket.jsf")
			|| req.getRequestURI().endsWith("consultarTicket.jsf")
			|| req.getRequestURI().endsWith("detalharTicket.jsf")
			|| req.getRequestURI().endsWith("listarTickets.jsf")
			|| req.getRequestURI().endsWith("verificarComprovantesPagamento.jsf")
			|| req.getRequestURI().endsWith("verificarComprovantesPagamento.jsf")
			|| req.getRequestURI().endsWith("verificarComprovantesPagamento.jsf")
			|| req.getRequestURI().contains("manterTicket/alterar/")
			|| req.getRequestURI().contains("manterTicket/excluir/")
			|| req.getRequestURI().contains("manterTicket/fechar/")
			|| req.getRequestURI().endsWith("verificarComprovantesPagamento.jsf")
			|| req.getRequestURI().endsWith("manterCategorias.jsf"));
	}
	private boolean paginasLiberadasCliente(HttpServletRequest req) {
		return (req.getRequestURI().endsWith("listarAlterarCliente.jsf")
			|| req.getRequestURI().endsWith("alterarClienteSessao.jsf")
			|| req.getRequestURI().endsWith("index.jsf")
			|| req.getRequestURI().endsWith("meuPerfil.jsf")
			|| req.getRequestURI().endsWith("contratarServico.jsf")
			|| req.getRequestURI().endsWith("historicoServico.jsf")
			|| req.getRequestURI().endsWith("consultarTicketCliente.jsf")
			|| req.getRequestURI().endsWith("meuPerfil.jsf")
			|| req.getRequestURI().endsWith("meuPerfil.jsf")
			|| req.getRequestURI().endsWith("listarAlterarCliente.jsf")
			|| req.getRequestURI().endsWith("alterarSenha.jsf")
			|| req.getRequestURI().endsWith("alterarServico.jsf")
			|| req.getRequestURI().contains("static")
			|| req.getRequestURI().contains("javax.faces.resource")
			|| req.getRequestURI().endsWith("enviarComprovantePagamento.jsf")
			|| req.getRequestURI().endsWith("alterarTicket.jsf")
			|| req.getRequestURI().endsWith("detalharContasCliente.jsf")
			|| req.getRequestURI().endsWith("abrirTicketCliente.jsf")
			|| req.getRequestURI().endsWith("sucessoAbeturaTicket.jsf")
			|| req.getRequestURI().endsWith("detalharTicket.jsf")
			|| req.getRequestURI().endsWith("listarTickets.jsf")
			|| req.getRequestURI().endsWith("manterCategorias.jsf"));
	}

	public void init(FilterConfig arg0) throws ServletException {
	}
}